﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XWG.JWBST.JWBSTEntity;
using XWG.Framework.AuthUtility;
using XWG.JWBST.JWBSTBLL;
using JWBST.CommonClass;

namespace XWG.JWBST.JWBSTManagementApp.ZPXXCJ
{
    public partial class WebShow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Page.RegisterStartupScript("提示", "<script language=javascript>window.history.go(1);</script>");
            }
        }

        protected void btn_askYZM_Click(object sender, EventArgs e)
        {
            //this.yzm.Value;
            string SerNum = this.texqu.Text.FilteHtml().Trim() + "公（" + this.texjc.Text.FilteHtml().Trim() + "）" + this.texjc0.Text.FilteHtml().Trim() + "字[" + this.texz.Text.FilteHtml().Trim() + "]第" + this.texh.Text.Trim().FilteHtml() + "号";
            TbBstCaseApply mTbBstCaseApply = new TbBstCaseApply();
            mTbBstCaseApply = TbBstCaseApplyBLL.Getapply(SerNum);
            if (mTbBstCaseApply != null)
            {
                //update
                ResultModel mResultModel = new ResultModel(); //初始化返回类型
                mTbBstCaseApply.Isaudit = "0";
                mTbBstCaseApply.Movesign = "0";
                mTbBstCaseApply.Code = this.yzm.Value;
                mResultModel = TbBstCaseApplyBLL.Update(mTbBstCaseApply, "WebUser", false);
                if (mResultModel.ISSucceed)
                {
                    this.LabMes.Text = "正在获取验证码。请耐心等待...";
                    this.btn_askYZM.Enabled = false;
                    //控件  不可修改
                    this.texh.ReadOnly = true;
                    this.texjc.ReadOnly = true;
                    this.texqu.ReadOnly = true;
                    this.texz.ReadOnly = true;
                }
            }
            else
            {
                //insert
                ResultModel mResultModel = new ResultModel(); //初始化返回类型
                TbBstCaseApply mmTbBstCaseApply = new TbBstCaseApply();
                mmTbBstCaseApply.Id = Guid.NewGuid().ToString(); //主键
                var dTime = DateTime.Now;
                var lsh = dTime.ToString("yyyyMMdd") + dTime.Millisecond.ToString() + dTime.ToString("hh") + dTime.ToString("ss") + dTime.ToString("mm");
                mmTbBstCaseApply.Username = lsh;
                mmTbBstCaseApply.Tel = DES.Encode(this.textel.Value.FilteHtml());
                mmTbBstCaseApply.Serialnumber = SerNum;
                mmTbBstCaseApply.Createdate = DateTime.Now;
                mmTbBstCaseApply.Isaudit = "0"; //是否验证通过  0 待审核  1 未通过  2通过
                mmTbBstCaseApply.Movesign = "0"; //同步标志
                mmTbBstCaseApply.Ipaddress = GetWebClientIp(); //ip地址
                mmTbBstCaseApply.Code = this.yzm.Value;
                mResultModel = TbBstCaseApplyBLL.Insert(mmTbBstCaseApply, "WebUser", true); //新建操作
                if (mResultModel.ISSucceed)
                {
                    this.btn_askYZM.Enabled = false;
                    this.LabMes.Text = "正在获取验证码。请耐心等待...";
                    //控件  不可修改
                    this.texh.ReadOnly = true;
                    this.texjc.ReadOnly = true;
                    this.texqu.ReadOnly = true;
                    this.texz.ReadOnly = true;
                }
            }
        }

        public void Log(string appid, string opType)
        {
            TbBstCaseLog log = new TbBstCaseLog();
            log.Id = Guid.NewGuid().ToString();
            log.OpDate = DateTime.Now.ToString();
            log.OpType = opType;
            log.Ipaddress = GetWebClientIp();
            log.Applyid = appid;
            TbBstCaseLogBLL.Insert(log, "", false);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string SerNum = this.texqu.Text.FilteHtml().Trim() + "公（" + this.texjc.Text.FilteHtml().Trim() + "）" + this.texjc0.Text.FilteHtml().Trim() + "字[" + this.texz.Text.FilteHtml().Trim() + "]第" + this.texh.Text.Trim().FilteHtml() + "号";
            TbBstCaseApply mTbBstCaseApply = new TbBstCaseApply();
            mTbBstCaseApply = TbBstCaseApplyBLL.GetapplyBySerandTel(SerNum, DES.Encode(this.textel.Value.FilteHtml()));
            if (mTbBstCaseApply != null)
            {
                TbBstCaseTodo todo = new TbBstCaseTodo();
                todo.Applyid = mTbBstCaseApply.Id;
                todo.Id = Guid.NewGuid().ToString();
                todo.Movesign = "0";
                todo.Status = "0";
                todo.Lastsearchdate = DateTime.Now;
                TbBstCaseTodoBLL.Insert(todo, "", false);
                Log(mTbBstCaseApply.Id, "1");
                Response.Redirect("../GAXX/TbBstReturnForUser.aspx?appid=" + mTbBstCaseApply.Id);
            }
        }


        #region 获取web客户端ip
        /// <summary>
        /// 获取web客户端ip
        /// </summary>
        /// <returns></returns>
        public static string GetWebClientIp()
        {

            string userIP = "未获取用户IP";

            try
            {
                if (System.Web.HttpContext.Current == null
            || System.Web.HttpContext.Current.Request == null
            || System.Web.HttpContext.Current.Request.ServerVariables == null)
                    return "";

                string CustomerIP = "";

                //CDN加速后取到的IP simone 090805
                CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
                if (!string.IsNullOrEmpty(CustomerIP))
                {
                    return CustomerIP;
                }

                CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];


                if (!String.IsNullOrEmpty(CustomerIP))
                    return CustomerIP;

                if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
                {
                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                    if (CustomerIP == null)
                        CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
                else
                {
                    CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

                }

                if (string.Compare(CustomerIP, "unknown", true) == 0)
                    return System.Web.HttpContext.Current.Request.UserHostAddress;
                return CustomerIP;
            }
            catch { }

            return userIP;

        }
        #endregion

    }
}